const Controllers = require('../controllers/Controller.js').controller;
const formatTime_2 = require("../../utils/util").formatTime_2
const app = getApp();
Page({
  data: {
    globalStype:'',
    couponDetail:{},
    shopId:'',
    couponId:'',
    userId:'',
    couponPushId:'',
    accountId: '',
    couponAccountId:'',
    backbool:true,
    sharereceivedata:''
  },
  //领取优惠券
  receive(e) {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    Controllers.receive({shopId: this.data.shopId,couponId: this.data.couponId,couponPushId:this.data.couponPushId}).then(res=>{
      if(res.code==1){
        wx.navigateBack({
          delta: 1
        })
        wx.showToast({
          title: "领取成功",
          icon: 'none',
          duration: 800
        });
        this.getDetails()
      }
    })
  },
  //获取详情
  getDetails(){
    if(this.data.accountId){
      Controllers.accountShareDetails({shopId: this.data.shopId,couponAccountId:this.data.couponAccountId}).then(res=>{
        if(res.code==1){
          res.data.couponList.forEach(element => {
            if(element.timeState == 1){
              if(element.effectiveStartTime)
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
            if(element.effectiveEndTime)
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          if(res.data.effectiveStartTime)
          res.data.effectiveStartTime = formatTime_2(new Date(res.data.effectiveStartTime.replace(/-/g, '/')))
          if(res.data.effectiveEndTime)
          res.data.effectiveEndTime = formatTime_2(new Date(res.data.effectiveEndTime.replace(/-/g, '/')))
          if(res.data.startTime)
          res.data.startTime = formatTime_2(new Date(res.data.startTime.replace(/-/g, '/')))
          if(res.data.endTime)
          res.data.endTime = formatTime_2(new Date(res.data.endTime.replace(/-/g, '/')))
          this.setData({
            couponDetail:res.data
          })
          // this.myInformation()
        }
      })
    }else{
      Controllers.details({shopId: this.data.shopId,couponId:this.data.couponId}).then(res=>{
        if(res.code==1){
          res.data.couponList.forEach(element => {
            if(element.timeState == 1){
              if(element.effectiveStartTime)
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
            if(element.effectiveEndTime)
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          if(res.data.effectiveStartTime)
          res.data.effectiveStartTime = formatTime_2(new Date(res.data.effectiveStartTime.replace(/-/g, '/')))
          if(res.data.effectiveEndTime)
          res.data.effectiveEndTime = formatTime_2(new Date(res.data.effectiveEndTime.replace(/-/g, '/')))
          if(res.data.startTime)
          res.data.startTime = formatTime_2(new Date(res.data.startTime.replace(/-/g, '/')))
          if(res.data.endTime)
          res.data.endTime = formatTime_2(new Date(res.data.endTime.replace(/-/g, '/')))
          this.setData({
            couponDetail:res.data
          })
          this.sharereceive()
          // this.myInformation()
        }
      })
    }
    
  },
  sharereceive(){
    Controllers.receive1({shopId: this.data.shopId,couponId: this.data.couponId,couponPushId:this.data.couponPushId}).then(res=>{
      if(res.code==1){
        this.setData({
          sharereceivedata:res.data
        })
      }
    }).catch(res=>{

    })
  },
  //分享或赠送
  onShareAppMessage: function (e) {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    if (this.data.couponDetail.couponAccount) {
      let title = this.data.couponDetail.name;
      let imageUrl = '';
      let path = '/pages/Mall/Coupondetail?shopId=' + this.data.shopId + '&accountId=' + this.data.couponDetail.couponAccount.accountId + '&id=' + this.data.couponDetail.couponAccount.couponId + '&couponAccountId=' + this.data.couponDetail.couponAccount.id
      console.log(path)
      let obj = {
        title,
        imageUrl,
        path,
        success: function (res) {

        },
        fail(err) {

        }
      }
      return obj
    } else {
      let title = this.data.couponDetail.name;
      let imageUrl = '';
      let path = '/pages/Mall/Coupongiftbag?shopId=' + this.data.shopId + '&accountId=' + this.data.sharereceivedata.accountId + '&id=' + this.data.sharereceivedata.couponId + '&couponAccountId=' + this.data.sharereceivedata.id
      let obj = {
        title,
        imageUrl,
        path,
        success: function (res) {

        },
        fail(err) {

        }
      }
      console.log(obj)
      return obj


    }

  },
  //领取赠送优惠券
  receiveGive() {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    Controllers.receiveGive({ accountId: this.data.accountId, shopId: this.data.shopId, couponAccountId: this.data.couponAccountId}).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: "领取成功",
          icon: 'none',
          duration: 800
        })
        this.getDetails()
      }
    })
  },
  gouse(){
    wx.navigateTo({
      url:'/pages/Index/Index'
    },1000)
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    if (app.globalData.shopId) {
      this.setData({
        shopId: app.globalData.shopId
      })
    }
    if(option.shopId){
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId=option.shopId
    }
    if(option.id){
      this.setData({
        couponId: option.id,
      })
    }
    if (option.accountId) {
      this.setData({
        accountId: option.accountId
      })
    }
    if (option.couponAccountId) {
      this.setData({
        couponAccountId: option.couponAccountId
      })
    }
    if (option.couponPushId){
      this.setData({
        couponPushId: option.couponPushId
      })
    }

    // if(option.accountId){
    //   this.receiveGive(option.accountId,option.shopId,option.couponAccountId)
    // }
    
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.getDetails()
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.getDetails()
      }
    }
  },
  // gopage(e){
  //   var url=e.currentTarget.dataset.url
  //   wx.navigateTo({
  //     url: url
  //   })
  // },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  },
  onShow() { },


})